package com.example.demo.ela.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.lucene.search.Sort;
import org.elasticsearch.action.admin.indices.analyze.AnalyzeAction;
import org.elasticsearch.action.admin.indices.analyze.AnalyzeRequestBuilder;
import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.suggest.SuggestRequestBuilder;
import org.elasticsearch.action.suggest.SuggestResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.elasticsearch.search.suggest.Suggest;
import org.elasticsearch.search.suggest.Suggest.Suggestion;
import org.elasticsearch.search.suggest.Suggest.Suggestion.Entry;
import org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestBuilders;
import org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.ela.DetailOrderDocumentRepository;
import com.example.demo.ela.OrderDocument;

@RestController
@RequestMapping(value = "/es/test")
public class EsTest {

	@Resource
	TransportClient transportClient;
	@Resource
	ElasticsearchTemplate elasticsearchTemplate;
	@Resource
	DetailOrderDocumentRepository  detailOrderDocumentRepository;
	@RequestMapping(value = "/add")
	public OrderDocument add(@RequestParam String id,@RequestParam String name) {
		//创建类
		/* 	DetailOrderDocument myEsEntity = new DetailOrderDocument();
	    	myEsEntity.setId("1");
	    	myEsEntity.setParentId("1");
	    	myEsEntity.setPrice(1L);*/
		//创建类
		OrderDocument detailOrderDocument =new OrderDocument();
		detailOrderDocument.setId(id);
		detailOrderDocument.setName(name);
		detailOrderDocument.setPrice(1l);
		detailOrderDocument.setNote("无常大米");
		OrderDocument save = detailOrderDocumentRepository.save(detailOrderDocument);
		return save;
	}
	@RequestMapping(value = "/get")
	public List<OrderDocument> get(@RequestParam String name) {
		Sort a =new Sort();
		List<OrderDocument> findByName = detailOrderDocumentRepository.findByName(name, a);
		return findByName;
	}


	@RequestMapping(value = "/getquery")
	public List<String> getquery(@RequestParam String name) {

		CompletionSuggestionBuilder suggestion = SuggestBuilders.completionSuggestion("complete").analyzer("ik_max_word").text(name).field("name").size(5);
		SuggestResponse actionGet = transportClient.prepareSuggest("orders-test").addSuggestion(suggestion).execute().actionGet(); 
		List<? extends Entry<? extends Option>> list = actionGet.getSuggest().getSuggestion("complete").getEntries(); 
		List<String> suggests = new ArrayList<String>(); 
		if (list == null) { 
			return null; 
		} else { 
			for (Entry<? extends Option> e : list) { 
				for (Option option : e) { 
					suggests.add(option.getText().toString()); 
				} 
			} 
			return suggests; 
		}
	}
	
	@RequestMapping(value = "/getquery1")
	public List<String> getquery1(@RequestParam String name) {
		
		 CompletionSuggestionBuilder suggestionBuilder = new CompletionSuggestionBuilder("firstName");
	        suggestionBuilder.analyzer("ik_smart");
	        suggestionBuilder.text(name);
	        suggestionBuilder.field("name");
	        SearchRequestBuilder response = transportClient.prepareSearch("orders-test").setTypes("order-document").setQuery(QueryBuilders.matchAllQuery());
	               
	        SearchResponse searchResponse = response.addSuggestion(suggestionBuilder).get();
	        Suggest suggest= searchResponse.getSuggest();
	        
	       List<? extends Entry<? extends Option>> entries = suggest.getSuggestion("firstName").getEntries();
	       
	       List<String> suggests = new ArrayList<String>(); 
			if (entries == null) { 
				return null; 
			} else { 
				for (Entry<? extends Option> e : entries) { 
					for (Option option : e) { 
						suggests.add(option.getText().toString()); 
					} 
				} 
				return suggests; 
			}
	}
	@RequestMapping(value = "/queryik")
	public List<String> query1(@RequestParam String name) {
		AnalyzeAction action;
	 AnalyzeRequestBuilder requestBuilder = new AnalyzeRequestBuilder(transportClient,   AnalyzeAction.INSTANCE, "orders-test", name);
	 requestBuilder.setTokenizer("ik");
	 List<AnalyzeResponse.AnalyzeToken> ikTokenList = requestBuilder.execute().actionGet().getTokens();
	 List<String> searchTermList = new ArrayList<>();

     ikTokenList.forEach(ikToken -> { searchTermList.add(ikToken.getTerm()); });


		return searchTermList;
		
		
	}
	
}